<template>
	<view class="mainPage">
		<view class="mainRegitst">
			<view class="mainLogo">
				<image src="../../static/logo.jpg"></image>
			</view>
			<view class="mainText">感谢使用上云丰小程序</view>
			<view class="mainText">选择您的身份注册类型,为您提优质的服务</view>
			<view class="mainItem" @click="handlerClick('planting',1)">我是种植户</view>
			<view class="mainItem" @click="handlerClick('maoyi',2)">我是贸易商</view>
			<view class="mainItem" @click="handlerClick('taobao',3)">我是淘宝店主</view>
			<view class="mainText" style="margin-top: 50rpx;">选择身份后不可更改,为了您的优质体验请准确选择</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '',
				shopUserId:''
			}
		},
		onLoad(e) {
			this.shopUserId = uni.getStorageSync('shopUserId') || ''
			// 获取code
				wx.login({
					success: res => {
						this.code = res.code;
						// uni.setStorageSync('AuthCode', res.code);
						console.log(res, '微信登录返回')
					}
				})
		},
		methods: {
			// 获取审批状态
			getUserState(){
				let self = this;
				self._post('user/user/detail', {
					source: self.getPlatform()
				}, function(res) {
					// AUDIT("审批中", 10),
					// NORMAL("正常", 20),
					// DISABLE("禁用", 30),
					// REFUSE("审批拒绝",40),
					uni.setStorageSync('UserState', res.data.status);
				});
			},
			handlerClick(name, type) {
				this.loginByAuthCode(this.code).then(res => {
					console.log(res, '返回')
					if (res) {
						// 成功就查询状态
						setTimeout(()=>{
							this.getUserState();
						},1000)
						if(this.shopUserId){
							uni.switchTab({
								url: '/pages/index/index',
							})
						}else{
							// 注册了返回
							uni.navigateBack();
						}
					
					} else {
						// 未注册跳转到注册页面
						uni.redirectTo({
							url: '/regpages/pages/' + name + '?type=' + type,
						})
					}
				})

			},
			// 判断身份
			loginByAuthCode(authCode) {
				return new Promise(
					(resolve, reject) => {
						this._post('user/userOpen/loginByAuthCode', {
							authCode
						}, (res) => {
							console.log(res, 'loginByAuthCode返回')
							const {
								data
							} = res;
							// 是否注册 未注册跳注册页面
							if (!data.registerFlag) {
								resolve(false)
							} else {
								// 否则直接登录
								uni.setStorageSync('token', data.token);
								resolve(true)
							}
						});
					}
				)

			},
		}

	}
</script>

<style scoped>
	.mainPage {
		display: flex;
		background: #fff;
		height: 100vh;

	}

	.mainRegitst {
		margin: 100rpx auto;
		width: 80%;
		padding: 10rpx;
		background: #fff;
	}

	.mainLogo {
		width: 150rpx;
		height: 150rpx;
		border-radius: 50%;
		margin: 20rpx auto 40rpx auto;
		border: 1px solid #19a352;
		padding: 15rpx;
	}

	.mainLogo image {
		width: 100%;
		height: 100%;

		border-radius: 50%;
	}

	.mainItem {
		height: 80rpx;
		color: #fff;
		background: #19a352;
		line-height: 80rpx;
		font-size: 29rpx;
		text-align: center;
		margin-top: 50rpx;
		border-radius: 10rpx;

	}

	.mainText {
		margin: 10rpx;
		text-align: center;
		color: #909399;

	}
</style>